package com.eastandroid.zoom;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Random;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.eastandroid.zoom.ZoomState.MinSize;
import com.eastandroid.zoom.ZoomState.Position;
import com.eastandroid.zoom.ZoomState.UserTouch;
import com.eastandroid.zoom.ZoomState.ZoomStyle;

public class ActivityZoom extends Activity {
	protected static final String TAG = "ActivityZoom";


	private ZoomView mZoomView;
	private ZoomStateView mZoomStateView;

	private Bitmap mBitmap;
	private ZoomState mZoomState;
	private GestureZoom mZoomGesture;
	private GestureDraw mDrawGesture;

	private static enum MODE {
		ZOOM, DRAW
	};
	private MODE mode = MODE.ZOOM;

	private Context mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mContext = this;
		setContentView(R.layout.main);

		mZoomState = new ZoomState();
		mZoomState.deleteObservers();

		mZoomGesture = new GestureZoom();
		mDrawGesture = new GestureDraw();
		GestureBase.setZoomState(mZoomState);

		mZoomView = (ZoomView) findViewById(R.id.zoomview);
		mZoomView.setZoomState(mZoomState);
		mZoomView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (mode) {
				case ZOOM:
					mZoomGesture.onTouchEvent(event);
					break;
				case DRAW:
					mDrawGesture.onTouchEvent(event);
					break;
				default:
					break;
				}

				return true;
			}
		});
		mZoomState.addObserver(mZoomView);

		mZoomStateView = (ZoomStateView) findViewById(R.id.zoomstateview);
		mZoomStateView.setZoomState(mZoomState);
		mZoomGesture.setOnEventDebugListener(mZoomStateView);
		mDrawGesture.setOnEventDebugListener(mZoomStateView);
		
	}

	@Override
	public void onPause() {
		super.onPause();

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mBitmap != null && !mBitmap.isRecycled())
		{
			mBitmap.recycle();
			mBitmap=null;
		}
		mZoomState.deleteObservers();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.control, menu);
		return super.onCreateOptionsMenu(menu);
	}
	Intent  	intent ;

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		item.setChecked(true);

		switch (item.getItemId()) {
		case R.id.reset:
			mZoomState.reset();
			break;
		case R.id.zoom:
			mode = MODE.ZOOM;
			break;
		case R.id.draw:
			mode = MODE.DRAW;
			break;

		case R.id.rect:
			mZoomState.setZoomStyle(ZoomStyle.RECT);
			break;
		case R.id.poly:
			mZoomState.setZoomStyle(ZoomStyle.POLY);
			break;

		case R.id.none:
			mZoomState.setMinSize(MinSize.NONE);
			break;
		case R.id.pgae:
			mZoomState.setMinSize(MinSize.PAGE);
			break;
		case R.id.width:
			mZoomState.setMinSize(MinSize.WIDTH);
			break;

		case R.id.pos_none:
			mZoomState.setPosition(Position.NONE);
			break;
		case R.id.pos_centerin:
			mZoomState.setPosition(Position.CENTERIN);
			break;
		case R.id.pos_inside:
			mZoomState.setPosition(Position.INSIDE);
			break;
			
		case R.id.touch_none:
			mZoomState.setUserTouch(UserTouch.NONE);
			break;
		case R.id.touch_ignore:
			mZoomState.setUserTouch(UserTouch.TOUCHIGNORE);
			break;
		case R.id.touch_after:
			mZoomState.setUserTouch(UserTouch.TOUCHAFTER);
			break;
			
		case R.id.file_load:
			getContent();
			break;
		case R.id.file_load_crop:
			getContentCrop();
			break;
		case R.id.file_image_capture:
			getContentCapture();
			break;
			
		default:
			break;
		}
		mZoomState.notifyObservers();
		return super.onOptionsItemSelected(item);
	}
	
	

	private static final int GET_IMAGE_URI = 1001;
	private static final int GET_IMAGE_CROP = 1002;  
	private static final int GET_IMAGE_CAMERA = 1003;
	private static Uri mTempFile =  Uri.fromFile( new File(Environment.getExternalStorageDirectory() + "/tempfile") );
	
	
	private void getContent(){
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType( "image/*");
		try {
			startActivityForResult(intent, GET_IMAGE_URI);
		} catch (ActivityNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private void getContentCrop() {
		Intent intent = new Intent(Intent.ACTION_PICK);
		intent.setType("image/*");
		try {
			startActivityForResult(intent, GET_IMAGE_CROP);
		} catch (ActivityNotFoundException e) {
			e.printStackTrace();
		}
	}

	private void getContentCrop(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP", uri);
		try {
			startActivityForResult(intent, GET_IMAGE_URI);
		} catch (ActivityNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	private void getContentCapture() {
		Intent inttPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT, mTempFile);
		try {
			startActivityForResult(inttPhoto, GET_IMAGE_CAMERA );
		} catch (ActivityNotFoundException e) {
			e.printStackTrace();
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK) {
			if (requestCode == GET_IMAGE_URI) {
				Uri uri = data.getData();
				try {
					mBitmap = Images.Media.getBitmap(mContext.getContentResolver(), uri);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (requestCode == GET_IMAGE_CROP) {
				getContentCrop(data.getData());
			}

			if (requestCode == GET_IMAGE_CAMERA) {
				try {
					mBitmap = Images.Media.getBitmap(mContext.getContentResolver(), mTempFile);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			if (mBitmap != null) {
				mZoomView.setImage(mBitmap);
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}
}
